/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.xre.protocol.testing.netty.websocket;

import org.jboss.netty.channel.ChannelPipelineFactory;

import com.hercules.xre.protocol.testing.EventHandler;
import com.hercules.xre.protocol.testing.Helper;
import com.hercules.xre.protocol.testing.netty.BaseProtocolTestingConnectionManager;
import com.hercules.xre.protocol.testing.netty.SSLEngineFactory;


/**
 * <p>
 * XRE connection manager that is responsible for handling communication trough web socket.
 * </p>
 * <p>
 * <strong>Thread-safety:</strong> This class isn't thread safe as it's parent isn't thread safe.
 * </p>
 *
 * @author TCSASSEMBLER
 * @version 1.0
 */
public class WebSocketProtocolTestingConnectionManager extends BaseProtocolTestingConnectionManager {
    /**
     * <p>
     * Creates new instance.
     * </p>
     *
     * @param sslEngineFactory
     *            ssl engine factory, can be null (if null no encryption will be used).
     * @param bindAddress
     *            address to which application should be bound, if null is passed then application will bind on all
     *            available interfaces
     * @param port
     *            port number on which connection manager should be listening. A valid port value is between 1 and
     *            65535.
     * @throws IllegalArgumentException
     *             when any of passed parameters is invalid
     */
    public WebSocketProtocolTestingConnectionManager(int port, String bindAddress, SSLEngineFactory sslEngineFactory) {
        super(Helper.checkPortAndLog("WebSocketProtocolTestingConnectionManager#ctor", new String[] {"port",
            "bindAddress", "sslEngineFactory" }, new Object[] {port, bindAddress, sslEngineFactory }, LOGGER),
                bindAddress, sslEngineFactory);
        final String signature = "WebSocketProtocolTestingConnectionManager#ctor";
        Helper.logExit(signature, null, LOGGER);
    }

    /**
     * <p>
     * Creates new instance of pipeline factory that should be used for handling web socket communication communication.
     * </p>
     *
     * @param eventHandler
     *            event handler that should be used to inform application about communication events, can't be null
     * @return WebSocketConnectionChannelPipelineFactory instance.
     */
    @Override
    protected ChannelPipelineFactory getPipelineFactory(EventHandler eventHandler) {
        final String signature = "WebSocketProtocolTestingConnectionManager#getPipelineFactory";
        Helper.logEnter(signature, new String[] {"eventHandler" }, new Object[] {eventHandler }, LOGGER);
        ChannelPipelineFactory res = null;
        try {
            Helper.checkNull(eventHandler, "eventHandler", signature, LOGGER);
            res = new WebSocketConnectionChannelPipelineFactory(this, eventHandler);
        } finally {
            Helper.logExit(signature, res, LOGGER);
        }
        return res;
    }
}
